from django.conf import settings
import requests
import json

import time
import Common

class SvcCommon:

    def reqSvc(self, uri = '', data = {}):
        SVC_HOST = settings.SVC_HOST
        SVC_IP = settings.SVC_IP

        url = SVC_IP + uri

        data_str = json.dumps(data, ensure_ascii=False).decode('utf8')

        log = 'uri: %s params: %s' % (uri, data_str)
        print log
        #DLOG(log_content=log, log_level='notice', log_name='req_svc')

        headers = {'Host' : SVC_HOST, 'Content-Type': 'application/x-www-form-urlencoded'}

        startTime = "%.4f" % (time.time())

        response = requests.post(url, data=data, headers=headers)

        endTime = "%.4f" % (time.time())
        runTime = int((float(endTime) - float(startTime)) * 1000)
        resCode = response.status_code


        log = 'code : %s res : %s [runtime = %d ms]' % (resCode, response.text, runTime)
        print log
        #DLOG(log_content=log, log_level='notice', log_name='req_svc')
        return json.loads(response.text)